package com.agriculture.monitoring.controller;

import com.agriculture.monitoring.dto.ApiResponse;
import com.agriculture.monitoring.dto.DeviceDTO;
import com.agriculture.monitoring.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author wms
 */
@RestController
@RequestMapping("/devices")
@CrossOrigin(origins = "*")
public class DeviceController {
    
    @Autowired
    private DeviceService deviceService;
    
    @GetMapping
    public ResponseEntity<ApiResponse<List<DeviceDTO>>> getAllDevices() {
        try {
            List<DeviceDTO> devices = deviceService.getAllDevices();
            return ResponseEntity.ok(ApiResponse.success("获取设备列表成功", devices));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("获取设备列表失败: " + e.getMessage()));
        }
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<DeviceDTO>> getDeviceById(@PathVariable Long id) {
        try {
            Optional<DeviceDTO> device = deviceService.getDeviceById(id);
            if (device.isPresent()) {
                return ResponseEntity.ok(ApiResponse.success("获取设备成功", device.get()));
            } else {
                return ResponseEntity.ok(ApiResponse.error("设备不存在"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("获取设备失败: " + e.getMessage()));
        }
    }
    
    @GetMapping("/code/{deviceCode}")
    public ResponseEntity<ApiResponse<DeviceDTO>> getDeviceByCode(@PathVariable String deviceCode) {
        try {
            Optional<DeviceDTO> device = deviceService.getDeviceByCode(deviceCode);
            if (device.isPresent()) {
                return ResponseEntity.ok(ApiResponse.success("获取设备成功", device.get()));
            } else {
                return ResponseEntity.ok(ApiResponse.error("设备不存在"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("获取设备失败: " + e.getMessage()));
        }
    }
    
    @GetMapping("/status/{status}")
    public ResponseEntity<ApiResponse<List<DeviceDTO>>> getDevicesByStatus(@PathVariable String status) {
        try {
            List<DeviceDTO> devices = deviceService.getDevicesByStatus(status);
            return ResponseEntity.ok(ApiResponse.success("获取设备列表成功", devices));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("获取设备列表失败: " + e.getMessage()));
        }
    }
    
    @PostMapping
    public ResponseEntity<ApiResponse<DeviceDTO>> createDevice(@RequestBody DeviceDTO deviceDTO) {
        try {
            DeviceDTO created = deviceService.createDevice(deviceDTO);
            return ResponseEntity.ok(ApiResponse.success("创建设备成功", created));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("创建设备失败: " + e.getMessage()));
        }
    }
    
    @PutMapping("/{id}/position")
    public ResponseEntity<ApiResponse<DeviceDTO>> updateDevicePosition(
            @PathVariable Long id,
            @RequestParam Double longitude,
            @RequestParam Double latitude) {
        try {
            Optional<DeviceDTO> updated = deviceService.updateDevicePosition(id, longitude, latitude);
            if (updated.isPresent()) {
                return ResponseEntity.ok(ApiResponse.success("更新位置成功", updated.get()));
            } else {
                return ResponseEntity.ok(ApiResponse.error("设备不存在"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("更新位置失败: " + e.getMessage()));
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteDevice(@PathVariable Long id) {
        try {
            boolean deleted = deviceService.deleteDevice(id);
            if (deleted) {
                return ResponseEntity.ok(ApiResponse.success("删除设备成功", null));
            } else {
                return ResponseEntity.ok(ApiResponse.error("设备不存在"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("删除设备失败: " + e.getMessage()));
        }
    }
}